(HSP3) sample8 [Webカメラ版 バーコード・QRコード読み取り]_x64.hsp

sample\hspdsc\64bit\(HSP3) sample8 [Webカメラ版 バーコード・QRコード読み取り]_x64.hsp » Plain Format

#include "hsp3_64.as"
#include "hspdsc64.as"
#include "hspbar64.as"

	title "Webカメラを使用したバーコードまたはQRコード読み取りサンプル"
	cls 4
	dsc_Init				// 初期化を必ずする!

	// リストのサイズを取得
	size = dsc_GetDeviceListSize()
	if size <= 0 : dialog "サイズ取得エラー" : end
	
	// バッファのサイズ取得し sdim で 確保
	sdim devlist, size
	
	// 利用可能なデバイス一覧取得
	dsc_GetDeviceList devlist
	if stat == 0 : dialog "デバイスが見つかりません" : end

	objsize 300, 20
	combox id, 100, devlist
	
	objsize 150,20
	button "読み取り開始", *play
	button "読み取り停止", *_stop
	button gosub "終了する", *exit
stop

*play

	// バーコード読み取り初期化
	bar_Init 1, 1, BarcodeFormat_Default	// JANコードとQRコードに対応
											// JANコード だけなら BarcodeFormat_JAN
											// QRコードだけなら BarcodeFormat_QRCode を指定する
	nIndex = stat

	// バーコード読み取り結果格納用変数
	size = bar_GetSize()
	dim res, size / 4
	
	// 操作ID 0 指定
	//(デフォルトでは0が指定されているため書かなくてもOK)
	dsc_SetActiveSel 0
	
	// デバイスのオープン
	dsc_Open id, 0, 0, 0, 0
	if stat < 0 : dialog "エラーが発生しました\nstat = " + stat, 1, "DEVICE-ERROR" : stop
	gsel 0, 1
	
	// デバイスの再生
	dsc_Play hwnd, 180, 120, 320, 240

	// 読み取り本体
	repeat

		wait 12

		// 下準備
		sdim dibdata, dsc_GetImageMemSize(0)
		sdim dibhead, dsc_GetImageMemSize(1)
		dibsize = dsc_GetImageMemSize(0)

		// カメラからキャプチャ
		dsc_GetImageMem dibdata, dibsize, dibhead
		x = lpeek(dibhead, 4)				// 幅
		y = lpeek(dibhead, 8)				// 高さ
		stride = -(x * 24 + 31) / 32 * 4	// Stride (24bit RGB)

		if ( x == 0 || y == 0 ) : continue	// サイズが 0 

		// キャプチャ結果をウィンドウへ描画
		if once == 0 : screen 3, x, y : title "キャプチャ結果" : once = 1
		gsel 3, 0 : mref vram, 66
		memcpy vram, dibdata, dibsize, 0, 0 
		redraw 1

		// バーコード読み込み
		bar_Read nIndex, ImageFormat_BGR, dibdata, x, y, stride, res
		if ( stat < 0 ) {
			dialog "読み込みエラー"
			bar_UnInit nIndex
			end
		} else : if ( 0 < stat ) {
			
			// バーコードまたはQRコードを発見!
			type = res(1)

			color 255, 0, 0 : pos 0, 0 
			if (type == BarcodeFormat_JAN) 		: mes "JAN形式のバーコード"
			if (type == BarcodeFormat_QRCode) 	: mes "QRコード"

			// 見つけたバーコードの矩形を描画
			color 0, 255, 0
			line res(2), res(3), res(4), res(5)	// 左上 - 右上
			line res(4), res(5), res(8), res(9)	// 右上 - 右下
			line res(2), res(3), res(6), res(7)	// 左上 - 左下
			line res(6), res(7), res(8), res(9)	// 左下 - 右下
			
			// テキスト取得
			bar_CopyTextU8_GetSize res, 0
			txtsize = stat
			
			sdim txt, txtsize
			bar_CopyTextU8 res, txt, txtsize
			
			mes txt

			// 角度(QRのみ)
			if ( type == BarcodeFormat_QRCode) {
				orientation = 0.0
				memcpy orientation, res, 8, 0, 40
				mes "" + rad2deg(orientation) + " 度"
			}

			// 解放
			bar_Free res

			redraw 1
		}

		gsel 0, 0

	loop
stop

*close
	// キャプチャ停止
	dsc_Stop
	dsc_Close
	// バーコード系解放
	bar_Free res
	bar_UnInit nIndex
return

*_stop
	gosub *close
stop

*exit
	gosub *close
	end
return